"""
Case Type   : gs_preupgradechk工具
Case Name   : gs_preupgradechk工具检测输出日志到指定文件
Create At   : 2025/11/17
Owner       : chen
Description :
    1.检查所有项指定文件
Expect      :
    1.文件有内容
History     :
"""

import unittest
import os
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

LOG = Logger()
primary_sh = CommonSH('PrimaryDbUser')

class Tools_Gs_Preupgradechk_TestCase0021(unittest.TestCase):
    def setUp(self):
        LOG.info(f'----{os.path.basename(__file__)} start----')
        self.pri_node = Node('PrimaryDbUser')
        self.com = Common()
        self.constant = Constant()
        self.log_file = 'log_preupgradechk_021'
        LOG.info("---SetUp：检查数据库状态是否正常---")
        status = primary_sh.get_db_cluster_status()
        LOG.info(status)
        self.assertTrue("Degraded" in status or "Normal" in status)

    def test_gs_preupgradechk_tools(self):
        text = '----step1: 检查所有项指定文件; ' \
            'expect: 成功----'
        LOG.info(text)
        LOG.info(text)
        cmd = f"gs_preupgradechk -i A -l {self.log_file}.log"
        LOG.info(cmd)
        result = self.pri_node.sh(f"source {macro.DB_ENV_PATH};"
                                    f"{cmd}").result()
        LOG.info(result)
        self.assertIn('Checking all item', result)
        
        cmd = f'cat {self.log_file}*'
        LOG.info(cmd)
        result = self.pri_node.sh(cmd).result()
        LOG.info(result)
        self.assertIn('disk', result)
        self.assertIn('CPU', result)
        self.assertIn('Memory', result)
        self.assertIn('replay', result)
        self.assertIn('process', result)
        self.assertIn('network', result)

    def tearDown(self):
        LOG.info('--------清理环境--------')
        status = primary_sh.get_db_cluster_status("detail")
        LOG.info(status)
        self.assertTrue("Degraded" in status or "Normal" in status)
        LOG.info(f"------{os.path.basename(__file__)} end------")
